package com.example.demo.Controller.User_Rate;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.Bean.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Entity.User.User;
import com.example.demo.ServiceImpl.User.UserServiceImpl;
import com.example.demo.ServiceImpl.User_Rate.UserRateServiceImpl;
import com.example.demo.Entity.User_Rate.UserRate;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/userRate")
public class UserRateController {
    @Resource
    private UserRateServiceImpl userRateService;
    @Resource
    private UserServiceImpl userService;

    @PostMapping
    public Boolean save(@RequestBody UserRate userRate) {
        if (userRate.getType().equals("指导老师")){
            userRate.setPercent("40");
        }else if(userRate.getType().equals("评阅老师")){
            userRate.setPercent("30");
        }else if(userRate.getType().equals("答辩老师")){
            userRate.setPercent("10");
        }
        LambdaQueryWrapper<UserRate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRate::getOid,userRate.getOid());
        wrapper.eq(UserRate::getUid,userRate.getUid());
        UserRate userRate1 = userRateService.getOne(wrapper);
        if(userRate1!=null){
            userRate.setId(userRate1.getId());
        }
        return userRateService.saveOrUpdate(userRate);
    }

    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable(value = "id") Integer id) {
        return userRateService.removeById(id);
    }

    @GetMapping
    public R findAll() {
        return R.ok().put("userRateList", userRateService.list());
    }

    @GetMapping("/{id}")
    public R findOne(@PathVariable(value = "id") Integer id) {
        LambdaQueryWrapper<UserRate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRate::getOid, id);
        List<UserRate> list = userRateService.list(queryWrapper);
        List<User> userList=new ArrayList<>();
        list.forEach(i-> userList.add(userService.getById(i.getUid())));
        return R.ok().put("UserList", userList).put("userRateList", list);
    }

    @GetMapping("/page")
    public R findPage(@RequestParam(value = "pageNum") Integer pageNum,
                      @RequestParam(value = "pageSize") Integer pageSize) {
        return R.ok().put("userRatePage", userRateService.page(new Page<>(pageNum, pageSize)));
    }

}